package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.RolePermissionDTO;
import cn.edu.sgu.www.pms.entity.Permission;
import cn.edu.sgu.www.pms.pager.impl.PermissionPager;
import cn.edu.sgu.www.pms.restful.JsonPage;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色权限管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/role_permission", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RolePermissionController {

    private final RolePermissionService rolePermissionService;

    @Autowired
    public RolePermissionController(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    @ApiOperation("一键搜权")
    @RequestMapping(path = "/grant", method = RequestMethod.POST)
    public JsonResult<Void> grant(
            @ApiParam(name = "id", value = "角色ID", required = true)
            @NotNull(message = "角色" + CustomMessages.ID_NOT_NULL) Integer roleId) {
        rolePermissionService.grant(roleId);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加角色权限")
    @RequestMapping(path = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated RolePermissionDTO rolePermissionDTO) {
        rolePermissionService.insert(rolePermissionDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("删除角色权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true),
            @ApiImplicitParam(name = "permissionIds", value = "角色权限ID列表", required = true)
    })
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public JsonResult<Void> delete(
            @NotNull(message = "角色" + CustomMessages.ID_NOT_NULL) Integer roleId,
            @RequestParam("permissionIds") List<String > permissionIds) {
        rolePermissionService.delete(roleId, permissionIds);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("分页查询角色权限列表")
    @RequestMapping(path = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Permission>> selectByPage(PermissionPager pager) {
        Page<Permission> page = rolePermissionService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}